<?php
/*
 * 中奖运行文件
 * @author  Sameal
 * @package ixdcw
 * @file    index.inc.php
 * @time    12-12-12 下午2:02
 */
defined('IN_IXDCW') or exit('Access Denied');
require DT_CORE.'/'.$module.'/common.inc.php';

$do_rule = new ErnieRuleModule();
$do_prize = new ErnieModule();

if ($_userid) {
	$user = $db->getOne("SELECT erniedate , erniedate2 FROM {$db->getPre()}member WHERE userid=$_userid");
	$date=timetodate($DT_TIME,3);
	$secondNo = $db->count("{$db->getPre()}ernieshare","`userid`='{$_userid}' AND `date`='{$date}'");

	if ($user['erniedate']!=timetodate($DT_TIME,3) || ($secondNo>=5 && $user['erniedate2']!=timetodate($DT_TIME,3))) {
		$rule=$do_rule->getPrizeList('status=3 and starttime<='.intval(date('H',$DT_TIME)).' and overtime>='.intval(date('H',$DT_TIME)).' and (used < mount or mount=0)');
		unset($userPrize);
		$userPrize['priority']=0;
		foreach($rule as $t_rule){
			if(rand(1,$t_rule['difficulty'])<=1){
				if($userPrize['priority']<=$t_rule['priority']){
					$userPrize=$t_rule;
				}
			}
		}
		$do_rule->update($userPrize['id']);
		//中奖操作
		if(isset($userPrize["moneyadd"])&&$userPrize["moneyadd"]!=0){
			$userinfo=$db->getOne("SELECT * FROM {$db->getPre()}member WHERE userid='$_userid'");
			$post['userid']=$_userid;
			$post['username']=$userinfo['username'];
			$post['prizeid']=$userPrize["id"];
			$post['prizename']=$userPrize["name"];
			$post['addmoney']=$userPrize["moneyadd"];
			$post['datetime']=timetodate($DT_TIME,3);
			$post['time']=$DT_TIME;
			$post['ip']=$DT_IP;
			$post['status']=3;
			$do_prize->add($post);

			//金钱增加
			money_add($userinfo['username'], $userPrize["moneyadd"]);
			//金钱变动记录
			money_record($userinfo['username'], $userPrize["moneyadd"], '站内', 'system', '摇奖获得', '奖品ID:'.$userPrize['id']);
			//发送站内信
			send_message($userinfo['username'],"恭喜您中得讯都车网{$post['prizename']}大奖","恭喜您中得讯都车网{$post['prizename']}大奖");

			$times = 0;
			if($user['erniedate']!=timetodate($DT_TIME,3)){
				$times = $times +1;
			}
			if($secondNo>=5 && $user['erniedate2']!=timetodate($DT_TIME,3)){
				$times = $times +1;
			}
			$times = $times -1;
			echo '{"times":'.$times.',"money":'.intval($userPrize["moneyadd"]).'}';
		}else{
			$times = 0;
			if($user['erniedate']!=timetodate($DT_TIME,3)){
				$times = $times +1;
			}
			if($secondNo>=5 && $user['erniedate2']!=timetodate($DT_TIME,3)){
				$times = $times +1;
			}
			$times = $times -1;
			echo '{"times":'.$times.',"money":0}';
		}

		//关闭该用户今日摇奖

		if($user['erniedate']!=timetodate($DT_TIME,3)){
			$sql = "erniedate='".timetodate($DT_TIME,3)."'";
		}else{
			$sql = "erniedate2='".timetodate($DT_TIME,3)."'";
		}
		$db->query(Sql::update($db->getPre().'member',"$sql","userid=$_userid"));

	}else{
		echo '{"times":0,"money":0}';
	}
}else{
	echo '{"times":0,"money":0}';
}
?>